package com.bykj.taketurns.findfragment.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;

import com.bykj.taketurns.R;
import com.bykj.taketurns.activity.BaseActivity;
import com.bykj.taketurns.findfragment.fragment.SecondaryTextFragment;
import com.bykj.taketurns.findfragment.fragment.SecondaryVideoFragment;
import com.bykj.taketurns.utils.Util_Rotate3DAnimation;

/**
 * Created by Administrator on 2017/8/15.
 */

public class SecondaryActivity extends BaseActivity {
    private FrameLayout framelayout;
    private CompletedAnimListener listener;

    @Override
    public void initView(Bundle savedInstanceState) {
        framelayout = findViewById(R.id.fl_secondary);
        ReplaceFragmentMethod();
    }

    /**
     * 加载初始进入Fragment的方法
     */
    private void ReplaceFragmentMethod() {
        FragmentTransaction tration = getSupportFragmentManager()
                .beginTransaction();
        SecondaryVideoFragment secondaryVideoFragment = new SecondaryVideoFragment();
        SecondaryTextFragment secondaryTextFragment = new SecondaryTextFragment();
        tration.add(R.id.fl_secondary, secondaryTextFragment, "text");
        tration.add(R.id.fl_secondary, secondaryVideoFragment, "video");

        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("bundle");
        secondaryVideoFragment.setArguments(bundle);
        tration.commit();
    }

    /**
     * 设置一个新的三维旋转的容器视图。只翻一般，然后设置新的现实内容
     *
     * @param zheng    一个判断机制 如果为true 则向右翻转，如果false则向左翻转
     * @param fragment 传入的片段
     * @param start    起始位置
     * @param end      结束位置
     */
    public void applyRotation(final boolean zheng, final Fragment fragment,
                              final float start, final float end, final Fragment before) {
        // Find the center of the container
        final float centerX = framelayout.getWidth() / 2.0f;
        final float centerY = framelayout.getHeight() / 2.0f;

        // Create a new 3D rotation with the supplied parameter
        // The animation listener is used to trigger the next animation
        final Util_Rotate3DAnimation rotation = new Util_Rotate3DAnimation(
                start, end, centerX, centerY, 310.0f, true);
        rotation.setDuration(150);
        rotation.setFillAfter(true);
        rotation.setInterpolator(new AccelerateInterpolator());
        rotation.setAnimationListener(new DisplayNextView(zheng, fragment, before));// 添加监听执行现实内容的切换
        framelayout.startAnimation(rotation);// 执行上半场翻转动画
    }


    /**
     * 执行完上半部分旋转之后，设置要显示的新的View然后继续执行下半部分旋转
     */
    private final class DisplayNextView implements Animation.AnimationListener {
        private final boolean mPosition;
        private final Fragment mfragment;
        private final Fragment mBefore;

        private DisplayNextView(boolean zheng, Fragment fragment, Fragment before) {
            mPosition = zheng;
            mfragment = fragment;
            this.mBefore = before;
        }

        public void onAnimationStart(Animation animation) {
        }

        public void onAnimationEnd(Animation animation) {
            framelayout.post(new SwapViews(mPosition, mfragment, mBefore));// 添加新的View
        }

        public void onAnimationRepeat(Animation animation) {
        }
    }

    /**
     * 添加要显示的新的View，并执行下半部分的旋转操作
     */
    private final class SwapViews implements Runnable {
        private final boolean mPosition;
        private final Fragment mfragment;
        private final Fragment mBefore;

        SwapViews(boolean position, Fragment fragment, Fragment before) {
            mPosition = position;
            mfragment = fragment;
            this.mBefore = before;

        }

        public void run() {
            final float centerX = framelayout.getWidth() / 2.0f;
            final float centerY = framelayout.getHeight() / 2.0f;
            Util_Rotate3DAnimation rotation;
            FragmentTransaction tration = getSupportFragmentManager()
                    .beginTransaction();

            if (mPosition) {
                rotation = new Util_Rotate3DAnimation(-95, 0, centerX, centerY,
                        310.0f, false);
            } else {
                rotation = new Util_Rotate3DAnimation(95, 0, centerX, centerY,
                        310.0f, false);
            }
            tration.show(mfragment);
            tration.hide(mBefore);
            tration.commit();
            rotation.setDuration(150);
            rotation.setFillAfter(true);
            rotation.setInterpolator(new DecelerateInterpolator());
            framelayout.startAnimation(rotation);
            rotation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    if (mfragment.isAdded()) {
                        listener.animEnd();
                    }
                }
                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            });

        }
    }

    //监听动画完成的接口
    public interface CompletedAnimListener {
        void animEnd();
    }

    public void setCompleteAnimListener(CompletedAnimListener listener) {
        this.listener = listener;
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public int getParentView() {
        return R.layout.activity_secondary;
    }

    @Override
    public void initToolBar() {

    }

    @Override
    protected void postData() {

    }

    @Override
    public void onClick(View view) {

    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e("zhulei","onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        finish();
    }

}
